<template>
	<view class="webview">
		<web-view :src="url" @message="handlePostMessage"></web-view>
	</view>
</template>

<script>
	import store from '@/store'
	export default {
		data() {
			return {
				url: 'https://static-b355b199-f26f-4bea-b13c-1b2c8fff663f.bspapp.com/webview_h5/webview.html'
			} 
		},
		onLoad(op) {
			console.log('op', op)
			// this.openWebview(op);
		},
		methods: {
			openWebview(op){
				// 在webview页面处理url参数
				// 根据explain标志信息处理
				if (op.explain == '个人信息') {
					this.url = op.url+'?platform=wechat&token=' + uni.getStorageSync('token') + '&key=' + uni.getStorageSync('rsaPk') + '&timeDiff=' + uni.getStorageSync('timeDiff')
					// 设置当前页面标题 跳转成功后会根据H5页面document.title来设置
					uni.setNavigationBarTitle({
						title: op.explain
					});
				}
				
				console.log('url', this.url)
			},
			handlePostMessage(data) {
				console.log("接收到消息：" + JSON.stringify(data.detail));
				let act = data.detail.data[0].action
				if(act === 'pdf'){
					this.open_pdf()
				}
			},
			open_pdf(){
				uni.downloadFile({
					url: 'https://static-b355b199-f26f-4bea-b13c-1b2c8fff663f.bspapp.com/testpdf.pdf', //仅为示例，并非真实的资源
					success: (res) => {
						console.log('下载pdf',res)
						if (res.statusCode === 200) {
							// console.log('下载成功');
							var filePath = res.tempFilePath;
							uni.openDocument({
							  filePath: filePath,
							  showMenu: true,
							  success: function (res) {
								console.log('打开文档成功');
							  }
							});
						}
					}
				});
			}
		}
	}
</script>

<style>
</style>
